home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / tools / sgisv.c < prev    next >
C/C++ Source or Header  |  1992-02-14  |  7KB  |  255 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/sgisv.c,v 1.10 92/02/10 19:04:12 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <gl.h>
  30. #include <stdio.h>
  31. #include "tiffio.h"
  32.  
  33. #define    streq(a,b)    (strcmp(a,b) == 0)
  34.  
  35. typedef unsigned char u_char;
  36. typedef unsigned long u_long;
  37.  
  38. u_long    rowsperstrip = (u_long) -1;
  39. int    compression = COMPRESSION_LZW;
  40. int    config = PLANARCONFIG_CONTIG;
  41. int    xmaxscreen;
  42. int    ymaxscreen;
  43. short    photometric = PHOTOMETRIC_RGB;
  44.  
  45. main(argc, argv)
  46.     int argc;
  47.     char **argv;
  48. {
  49.  
  50.     argc--, argv++;
  51.     for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  52.         if (streq(argv[0], "-none")) {
  53.             compression = COMPRESSION_NONE;
  54.             continue;
  55.         }
  56.         if (streq(argv[0], "-packbits")) {
  57.             compression = COMPRESSION_PACKBITS;
  58.             continue;
  59.         }
  60.         if (streq(argv[0], "-lzw")) {
  61.             compression = COMPRESSION_LZW;
  62.             continue;
  63.         }
  64.         if (streq(argv[0], "-contig")) {
  65.             config = PLANARCONFIG_CONTIG;
  66.             continue;
  67.         }
  68.         if (streq(argv[0], "-separate")) {
  69.             config = PLANARCONFIG_SEPARATE;
  70.             continue;
  71.         }
  72.         if (streq(argv[0], "-rowsperstrip")) {
  73.             argc--, argv++;
  74.             rowsperstrip = atoi(argv[0]);
  75.             continue;
  76.         }
  77.         usage();
  78.     }
  79.     if (strcmp(argv[argc-1], "-b") == 0) {
  80.         argc--;
  81.         photometric = PHOTOMETRIC_MINISBLACK;
  82.     }
  83.     if (argc != 1 && argc != 5)
  84.         usage();
  85.     xmaxscreen = getgdesc(GD_XPMAX)-1;
  86.     ymaxscreen = getgdesc(GD_YPMAX)-1;
  87.     foreground();
  88.     noport();
  89.     winopen("tiffsv");
  90.     if (argc == 5)
  91.         tiffsv(argv[0],
  92.             atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  93.     else
  94.         tiffsv(argv[0], 0, xmaxscreen, 0, ymaxscreen);
  95. }
  96.  
  97. usage()
  98. {
  99.  
  100.     fprintf(stderr, "usage: tiffsv [options] outimage [x1 x2 y1 y2] [-b]\n");
  101.     fprintf(stderr, "where options are:\n");
  102.     fprintf(stderr,
  103.         " -contig\tpack samples contiguously (e.g. RGBRGB...)\n");
  104.     fprintf(stderr,
  105.         " -separate\tstore samples separately (e.g. RRR...GGG...BBB...)\n");
  106.     fprintf(stderr, "\n");
  107.     fprintf(stderr,
  108.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  109.     fprintf(stderr,
  110.         " -packbits\tcompress output with packbits encoding\n");
  111.     fprintf(stderr,
  112.         " -none\t\tuse no compression algorithm on output\n");
  113.     fprintf(stderr, "\n");
  114.     fprintf(stderr,
  115.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  116.     exit(-1);
  117. }
  118.  
  119. static
  120. svRGBSeparate(tif, ss, xsize, ysize)
  121.     TIFF *tif;
  122.     register u_long *ss;
  123.     int xsize, ysize;
  124. {
  125.     register int x, y;
  126.     int scanline = TIFFScanlineSize(tif);
  127.     u_char *rbuf = (u_char *)malloc(3*scanline);
  128.     u_char *gbuf = rbuf + scanline;
  129.     u_char *bbuf = gbuf + scanline;
  130.  
  131.     for (y = 0; y <= ysize; y++) {
  132.         for (x = 0; x <= xsize; x++) {
  133.             u_long v = ss[x];
  134.             rbuf[x] = v;
  135.             gbuf[x] = v >> 8;
  136.             bbuf[x] = v >> 16;
  137.         }
  138.         if (TIFFWriteScanline(tif, rbuf, y, 0) < 0 ||
  139.             TIFFWriteScanline(tif, gbuf, y, 1) < 0 ||
  140.             TIFFWriteScanline(tif, bbuf, y, 2) < 0)
  141.             break;
  142.         ss += xsize+1;
  143.     }
  144.     free(rbuf);
  145. }
  146.  
  147. static
  148. svRGBContig(tif, ss, xsize, ysize)
  149.     TIFF *tif;
  150.     register u_long *ss;
  151.     int xsize, ysize;
  152. {
  153.     register int x, y;
  154.     u_char *scanline = (u_char *)malloc(TIFFScanlineSize(tif));
  155.  
  156.     for (y = 0; y <= ysize; y++) {
  157.         register u_char *pp = scanline;
  158.  
  159.         for (x = 0; x <= xsize; x++) {
  160.             u_long v = ss[x];
  161.             pp[0] = v;
  162.             pp[1] = v >> 8;
  163.             pp[2] = v >> 16;
  164.             pp += 3;
  165.         }
  166.         if (TIFFWriteScanline(tif, scanline, y, 0) < 0)
  167.             break;
  168.         ss += xsize+1;
  169.     }
  170.     free(scanline);
  171. }
  172.  
  173. #undef RED
  174. #undef GREEN
  175. #undef BLUE
  176. #define    CVT(x)    (((x)*255)/100)
  177. #define    RED    CVT(28)        /* 28% */
  178. #define    GREEN    CVT(59)        /* 59% */
  179. #define    BLUE    CVT(11)        /* 11% */
  180.  
  181. static
  182. svGrey(tif, ss, xsize, ysize)
  183.     TIFF *tif;
  184.     register u_long *ss;
  185.     int xsize, ysize;
  186. {
  187.     register int x, y;
  188.     u_char *buf = (u_char *)malloc(TIFFScanlineSize(tif));
  189.  
  190.     for (y = 0; y <= ysize; y++) {
  191.         for (x = 0; x <= xsize; x++) {
  192.             u_char *cp = (u_char *)&ss[x];
  193.             buf[x] = (RED*cp[3] + GREEN*cp[2] + BLUE*cp[1]) >> 8;
  194.         }
  195.         if (TIFFWriteScanline(tif, buf, y, 0) < 0)
  196.             break;
  197.         ss += xsize+1;
  198.     }
  199.     free(buf);
  200. }
  201.  
  202. #define    MIN(a,b)    ((a)<(b)?(a):(b))
  203. #define    ABS(x)        ((x)<0?-(x):(x))
  204.  
  205. tiffsv(name, x1, x2, y1, y2)
  206.     char *name;
  207.     int x1, x2, y1, y2;
  208. {
  209.     TIFF *tif;
  210.     int xsize, ysize;
  211.     int xorg, yorg;
  212.     int temp, y, i;
  213.     int pos, togo, n;
  214.     u_long *scrbuf, *ss;
  215.  
  216.     xorg = MIN(x1,x2);
  217.     yorg = MIN(y1,y2);
  218.     if (xorg<0)
  219.         xorg = 0;
  220.     if (yorg<0)
  221.         yorg = 0;
  222.     xsize = ABS(x2-x1);
  223.     ysize = ABS(y2-y1);
  224.     if (xorg+xsize > xmaxscreen)
  225.         xsize = xmaxscreen-xorg;
  226.     if (yorg+ysize > ymaxscreen)
  227.         ysize = ymaxscreen-yorg;
  228.     tif = TIFFOpen(name, "w");
  229.     TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (u_long) (xsize+1));
  230.     TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (u_long) (ysize+1));
  231.     TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  232.     TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
  233.         photometric == PHOTOMETRIC_RGB ? 3 : 1);
  234.     TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
  235.     TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config);
  236.     TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
  237.     TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
  238.     if ((long)rowsperstrip <= 0)
  239.         rowsperstrip = (8*1024)/TIFFScanlineSize(tif);
  240.     TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
  241.         rowsperstrip == 0 ? 1L : rowsperstrip);
  242.     scrbuf = (u_long *)malloc((xsize+1)*(ysize+1)*sizeof (u_long));
  243.     readdisplay(xorg, yorg, xorg+xsize, yorg+ysize, scrbuf, RD_FREEZE);
  244.     ss = scrbuf;
  245.     if (photometric == PHOTOMETRIC_RGB) {
  246.         if (config == PLANARCONFIG_SEPARATE)
  247.             svRGBSeparate(tif, scrbuf, xsize, ysize);
  248.         else
  249.             svRGBContig(tif, scrbuf, xsize, ysize);
  250.     } else
  251.         svGrey(tif, scrbuf, xsize, ysize);
  252.     (void) TIFFClose(tif);
  253.     free((char *)scrbuf);
  254. }
  255.